#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>

int main(int argc, char *argv[])
{
	struct epoll_event event;
    struct epoll_event wait_event;
	
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in my_addr;
	bzero(&my_addr, sizeof(my_addr));
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(8001);
	my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
	listen(sockfd, 10);
	
	int fd[100];
	int i = 0, maxi = 0;
	memset(fd, -1, sizeof(fd));
	fd[0] = sockfd;

	int epfd = epoll_create(10);
    if( -1 == epfd )
    {
        perror ("epoll_create");
        return -1;
    }

    event.data.fd = sockfd;
    event.events = EPOLLIN;
	
    int ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
    if(-1 == ret)
    {
        perror("epoll_ctl");
        return -1;  
    } 

	while(1)
	{
		// 监视并等待多个文件（标准输入，udp套接字）描述符的属性变化（是否可读）  
        // 没有属性变化，这个函数会阻塞，直到有变化才往下执行，这里没有设置超时   
        ret = epoll_wait(epfd, &wait_event, maxi+1, -1);
		if((sockfd == wait_event.data.fd)       // 监测sockfd(监听套接字)是否存在连接
            && (EPOLLIN == wait_event.events & EPOLLIN))
		{
			struct sockaddr_in cli_addr;
			int clilen = sizeof(cli_addr);
			int connfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);     // 从tcp完成连接中提取客户端
			for(i = 1; i < OPEN_MAX; i++)       // 将提取到的connfd放入fd数组中，以便下面轮询客户端套接字
			{
				if(fd[i] < 0)
				{
					fd[i] = connfd;
					event.data.fd = connfd;
					event.events = EPOLLIN;
					ret = epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &event);       // 事件注册函数，将监听套接字描述符 connfd 加入监听事件
					if(-1 == ret)
                    {  
						perror("epoll_ctl");  
						return -1;  
					}
					break;
				}
			}

			if(i > maxi) maxi = i;      // maxi更新
			if(--ret <= 0) continue;    // 如果没有就绪的描述符，就继续epoll监测，否则继续向下看
		}

		for(i = 1; i <= maxi; i++)      // 继续响应就绪的描述符
		{
			if(fd[i] < 0) continue;
			if((fd[i] == wait_event.data.fd)
                && (EPOLLIN == wait_event.events & (EPOLLIN|EPOLLERR)))
			{
				int len = 0;
				char buf[128] = "";
				if((len = recv(fd[i], buf, sizeof(buf), 0)) < 0)
				{
					if(errno == ECONNRESET)                         // tcp连接超时、RST
					{
						close(fd[i]);
						fd[i] = -1;
					}
					else
                    {
                        perror("read error:");
                    }
				}
				else if(len == 0)       // 客户端关闭连接
				{
					close(fd[i]);
					fd[i] = -1;
				}
				else                    // 正常接收到服务器的数据
				{
                    send(fd[i], buf, len, 0);
                }

				if(--ret <= 0) break;   // 所有的就绪描述符处理完了，就退出当前的for循环，继续poll监测
			}
		}
	}
    
	return 0;
}